package com.lxit.cms.action.directive;

import static com.lxit.cms.Constants.TPL_STYLE_LIST;
import static com.lxit.cms.Constants.TPL_SUFFIX;
import static com.lxit.cms.web.FrontUtils.PARAM_STYLE_LIST;
import static com.lxit.common.web.Constants.UTF8;
import static com.lxit.common.web.freemarker.DirectiveUtils.OUT_LIST;
import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.lxit.cms.entity.assist.CmsGuestbookCtg;
import com.lxit.cms.entity.main.CmsSite;
import com.lxit.cms.manager.assist.CmsGuestbookCtgMng;
import com.lxit.cms.web.FrontUtils;
import com.lxit.common.web.freemarker.DirectiveUtils;
import com.lxit.common.web.freemarker.DirectiveUtils.InvokeType;
import com.lxit.common.web.freemarker.ParamsRequiredException;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 专题列表标签
 * 
 * @author liufang
 * 
 */
public class CmsGuestbookCtgListDirective implements TemplateDirectiveModel {
  /**
   * 模板名称
   */
  public static final String TPL_NAME = "geustbook_ctg_list";

  /**
   * 输入参数，站点ID。
   */
  public static final String PARAM_SITE_ID = "siteId";
  public static final String TYPE = "type";

  @SuppressWarnings("unchecked")
  public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    CmsSite site = FrontUtils.getSite(env);
    List<CmsGuestbookCtg> list = new ArrayList<CmsGuestbookCtg>();
    Integer flag = getType(params);
    if (flag != null) {
      if (flag < 21) {//章阁
        list = cmsGuestbookCtgMng.getListMinMax(getSiteId(params), 1, 20);
      } else if (flag < 90) {//福民(81-83)
        list = cmsGuestbookCtgMng.getListMinMax(getSiteId(params), 70, 90);
      } else {//新澜(91-93)
        list = cmsGuestbookCtgMng.getListMinMax(getSiteId(params), 90, 99);
      }
    } else {
      list = cmsGuestbookCtgMng.getList(getSiteId(params));

    }
    Map<String, TemplateModel> paramWrap = new HashMap<String, TemplateModel>(params);
    paramWrap.put(OUT_LIST, DEFAULT_WRAPPER.wrap(list));
    Map<String, TemplateModel> origMap = DirectiveUtils.addParamsToVariable(env, paramWrap);
    InvokeType type = DirectiveUtils.getInvokeType(params);
    String listStyle = DirectiveUtils.getString(PARAM_STYLE_LIST, params);
    if (InvokeType.sysDefined == type) {
      if (StringUtils.isBlank(listStyle)) {
        throw new ParamsRequiredException(PARAM_STYLE_LIST);
      }
      env.include(TPL_STYLE_LIST + listStyle + TPL_SUFFIX, UTF8, true);
    } else if (InvokeType.userDefined == type) {
      if (StringUtils.isBlank(listStyle)) {
        throw new ParamsRequiredException(PARAM_STYLE_LIST);
      }
      FrontUtils.includeTpl(TPL_STYLE_LIST, site, env);
    } else if (InvokeType.custom == type) {
      FrontUtils.includeTpl(TPL_NAME, site, params, env);
    } else if (InvokeType.body == type) {
      body.render(env.getOut());
    } else {
      throw new RuntimeException("invoke type not handled: " + type);
    }
    DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap);
  }

  private Integer getSiteId(Map<String, TemplateModel> params) throws TemplateException {
    return DirectiveUtils.getInt(PARAM_SITE_ID, params);
  }

  private Integer getType(Map<String, TemplateModel> params) throws TemplateException {
    return DirectiveUtils.getInt(TYPE, params);
  }

  @Autowired
  private CmsGuestbookCtgMng cmsGuestbookCtgMng;
}
